package com.ruoyi.typing.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.system.service.ISysDeptService;

import com.ruoyi.typing.domain.Article;
import com.ruoyi.typing.domain.Exam;
import com.ruoyi.typing.domain.Score;
import com.ruoyi.typing.domain.Student;
import com.ruoyi.typing.service.IArticleService;
import com.ruoyi.typing.service.IExamService;
import com.ruoyi.typing.service.IScoreService;
import com.ruoyi.typing.service.IStudentService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 首页 业务处理
 *
 * @author ruoyi
 */
@Controller
public class IndexController extends BaseController
{
    private String prefix = "typing/article";
    
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IExamService examService;
    @Autowired
    private ISysDeptService deptService;

    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        String webIndex = "typing/index";
        return webIndex;
    }

    @GetMapping("/exam")
    public String exam(ModelMap mmap)
    {
        String webIndex = "typing/exam";
        return webIndex;
    }

    @PostMapping("/listExam")
    @ResponseBody
    public TableDataInfo listExam()
    {
        startPage();
        Exam exam= new Exam();
        exam.setActive(1);
        List<Exam> list = examService.selectExamList(exam);
        return getDataTable(list);
    }

    @GetMapping("/getExamById/{examId}")
    @ResponseBody
    public AjaxResult getExamById(@PathVariable("examId") Long examId)
    {
        Exam exam = examService.selectExamByExamId(examId);
        System.out.println(exam);
        return AjaxResult.success(AjaxResult.DATA_TAG,exam);
    }

    @GetMapping("/getArticleById/{articleId}")
    @ResponseBody
    public AjaxResult getArticleById(@PathVariable("articleId") Long articleId)
    {
        Article article = this.articleService.selectArticleByArticleId(articleId);
        return AjaxResult.success(AjaxResult.DATA_TAG,article);
    }

    @PostMapping("/listArticle")
    @ResponseBody
    public TableDataInfo listArticle(@RequestBody Article article)
    {
        startPage();
        article.setActive(1);
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    @GetMapping("/status")
    public String status(ModelMap mmap)
    {
        String webIndex = "typing/status";
        return webIndex;
    }

    @GetMapping("/paiming")
    public String paiming(ModelMap mmap)
    {
        String webIndex = "typing/paiming";
        return webIndex;
    }

    @PostMapping("/listPaiming")
    @ResponseBody
    public TableDataInfo listPaiming(Score score)
    {
        startPage();
        List<Score> list = scoreService.selectScoreList(score);
        return getDataTable(list);
    }

    /**
     * 新增保存成绩
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody Score score)
    {
        //System.out.println(score);
        score.setScore(score.getSpeed()*score.getAccuracy()/100);
        score.setCreatedTime(new Date());

        Student stu = new Student();

        stu.setJiaoyujuId(score.getJiaoyujuId());
        stu.setJituanxiaoId(score.getJituanxiaoId());
        stu.setSchoolId(score.getSchoolId());

        stu.setNianji(score.getNianji());
        stu.setBanji(score.getBanji());
        stu.setName(score.getName());

        List<Student> students = studentService.selectStudentList(stu);
        if(students.size() == 0){
            Student student = new Student();

            if(score.getModel().equals("练习")){
                student.setScore(score.getScore());
                student.setSpeed(score.getSpeed());
                student.setAccuracy(score.getAccuracy());
            }else if(score.getModel().equals("考试")){
                student.setExamScore(score.getScore());
                student.setExamSpeed(score.getSpeed());
                student.setExamAccuracy(score.getAccuracy());
            }


            student.setName(score.getName());
            student.setNianji(score.getNianji());
            student.setBanji(score.getBanji());

            student.setJiaoyujuId(score.getJiaoyujuId());
            student.setJituanxiaoId(score.getJituanxiaoId());
            student.setSchoolId(score.getSchoolId());

            SysDept deptJiaoyuju = deptService.selectDeptById(score.getJiaoyujuId());
            student.setJiaoyuju(deptJiaoyuju.getDeptName());
            SysDept deptJituanxiao = deptService.selectDeptById(score.getJituanxiaoId());
            student.setJituanxiao(deptJituanxiao.getDeptName());
            SysDept deptSchool = deptService.selectDeptById(score.getSchoolId());
            student.setSchool(deptSchool.getDeptName());

            student.setActive(1);

            student.setCishu(1L);

            studentService.insertStudent(student);
        }else{
            Student student = students.get(0);

            student.setCishu(1 + student.getCishu());

            if(score.getModel().equals("练习") && student.getScore() < score.getScore()){
                student.setScore(score.getScore());
                student.setSpeed(score.getSpeed());
                student.setAccuracy(score.getAccuracy());
                studentService.updateStudent(student);
            }else if(score.getModel().equals("考试") && student.getExamScore() < score.getScore()){
                student.setExamScore(score.getScore());
                student.setExamSpeed(score.getSpeed());
                student.setExamAccuracy(score.getAccuracy());
                studentService.updateStudent(student);
            }
        }
        return toAjax(scoreService.insertScore(score));
    }

    @GetMapping("/student")
    public String student(ModelMap mmap)
    {
        String webIndex = "typing/student";
        return webIndex;
    }

    @PostMapping("/listStudent")
    @ResponseBody
    public TableDataInfo listStudent(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    @PostMapping("/listSysDept")
    @ResponseBody
    public List<SysDept> listSysDept(@RequestBody SysDept dept)
    {
        List<SysDept> deptList = deptService.selectDeptList(dept);
        return deptList;
    }

    @PostMapping("/checkExamScore")
    @ResponseBody
    public AjaxResult checkExamScore(@RequestBody Score score)
    {
        List<Score> scores = this.scoreService.selectScoreList(score);
        if(scores.size() == 0)
            return AjaxResult.success();
        else
            return AjaxResult.error("每次考试只能参加一次，你已经参加过本考试，不能再次参加");
    }

}
